package com.javasm.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.javasm.domin.entity.SaleOutDepot;
import com.javasm.domin.entity.SodDetailConn;
import com.javasm.domin.entity.SodSn;
import com.javasm.domin.vo.SodDetailConnVo;
import com.javasm.mapper.base.MyMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author whl
 * @since 2022-01-17
 */
public interface SaleOutDepotMapper extends MyMapper<SaleOutDepot> {
    String querySql = "select sot.*,i.indent_type,i.pay_type,i.vip_number from sale_out_depot sot,indent i where sot.indent_id = i.id";
    String wrapperSql = "SELECT * from ( " + querySql + " ) AS q ${ew.customSqlSegment}";

    @Select(wrapperSql)
    List<SaleOutDepot> selectTable(@Param("ew") Wrapper queryWrapper);

    @Select(querySql+" and sot.id = ${id}")
    SaleOutDepot getSodById(@Param("id") Long id);


    @Select("SELECT gdr.* ,gag.gifts_id, gag.gifts_name " +
            "FROM (SELECT gd.*,gdc.color_name FROM (select cg.*,COUNT(goods_id) num from count_goods cg where cg.id = ${id} GROUP BY cg.goods_id) gd," +
            "(SELECT c.color_name,g.goods_id FROM gd_goods g,gd_color c WHERE g.product_color_id = c.id ) gdc WHERE gd.goods_id = gdc.goods_id) gdr " +
            "Left Join gifts_and_goods gag ON gdr.goods_id = gag.goods_id")
    List<SodDetailConn> getSodDetail(@Param("id") Long id);

    @Select("SELECT s1.gd_id,s1.sn_code gd_sn,s2.sn_code gf_sn" +
            " FROM sale_out_sn s1" +
            " LEFT JOIN sale_out_sn s2 on s2.gd_id = s1.gd_id" +
            " AND s2.sod_id = ${id} AND s2.is_gd = 0" +
            " WHERE s1.sod_id = ${id} AND s1.is_gd = 1")
    List<SodSn> getSodSnById(@Param("id") Long id);
}
